package com.dominator.utils.pay;

import com.dominator.utils.encode.Des3Utils;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.system.Constants;
import com.dominator.utils.system.PropertiesLoader;
import com.dominator.utils.system.SeemseUtil;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 微信下单
 */
public class WxUnionOrder {
	public static PropertiesLoader propertiesLoader = new PropertiesLoader("wxconfig.properties");



	/**
	 * 微信预支付订单
	 * @return ApiMessage
	 * @throws ApiException
	 */
	public static Map<String, String> createPrePayOrder(SortedMap<String, Object> signMap, String trade_type,
			String openid) throws ApiException {

		// 生成订单号
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", signMap.get("appid").toString());// 应用ID
		map.put("body", signMap.get("body") + "");// 商品描述
		map.put("mch_id", signMap.get("mch_id").toString());// 商户号
		map.put("nonce_str", signMap.get("nonce_str") + "");// 随机字符串
		map.put("notify_url", signMap.get("notify_url") + "");// 通知地址
		map.put("openid", openid);
		map.put("out_trade_no", signMap.get("out_trade_no") + "");// 商户订单号
		map.put("sign_type", "MD5");
		map.put("spbill_create_ip", signMap.get("spbill_create_ip") + "");// 终端IP
		map.put("total_fee", signMap.get("total_fee") + "");// 总金额
		map.put("trade_type", signMap.get("trade_type") + "");// 交易类型
		map.put("sign", signMap.get("sign") + "");
		// 用户唯一标识符
		// 进行xml格式拼接
		String xml = SeemseUtil.createXml(map);
		System.out.println(xml);
		// 接收接口返回值
		String returnXml = HttpKit.post(propertiesLoader.getProperty("wx_prepay_url"), xml);
		System.out.println("获取订单成功");
		System.out.println(returnXml);
		// 解析xml格式数据
		Element root = XmlParser.doParse(returnXml);
		System.out.println(root.toString());
		// 获取返回状态
		String returnCode = root.elementTextTrim("return_code");
		String resultCode = root.elementText("result_code");
		String prepay_id = "";
		String code = "ERROR";
		System.out.println(returnCode + "------" + resultCode);
		Map<String, String> resMap = new HashMap<String, String>();
		if (returnCode.equals("SUCCESS") && resultCode.equals("SUCCESS")) {
			System.err.println("结果验证");
			long currentTimeMillis = System.currentTimeMillis();// 生成时间戳
			System.err.println(currentTimeMillis);
			long second = currentTimeMillis / 1000L;// （转换成秒）
			resMap.put("nonce_str", map.get("nonce_str"));
			resMap.put("prepay_id", "prepay_id=" + root.elementText("prepay_id"));
			resMap.put("out_trade_no", signMap.get("out_trade_no") + "");
			resMap.put("appId", map.get("appid") + "");
			resMap.put("timeStamp", second + "");
			resMap.put("signType", "MD5");
			// 二次签名（微信公众号支付传参paySign需要nonceStr、package、appId、timeStamp、signType参与签名）
			SortedMap<String, Object> signMaps = new TreeMap<String, Object>();
			signMaps.put("nonceStr", map.get("nonce_str"));
			signMaps.put("package", "prepay_id=" + root.elementText("prepay_id"));
			signMaps.put("appId", map.get("appid") + "");
			signMaps.put("timeStamp", second + "");
			signMaps.put("signType", "MD5");
			String sign = WxPaySign.createSign(signMaps, propertiesLoader.getProperty("wx_public_key"));
			resMap.put("sign", sign);
			System.err.println("22222" + Des3Utils.encResponse(resMap));
		}else {
			throw new ApiException(Constants.REQ_ORDER_ERROR, "获取订单失败");
		}
		return resMap;

	}

	/**
	 * 微信签名(公众号)
	 *
	 * @param total_fee(支付金额)；
	 * @param spbill_create_ip（用户端实际ip）
	 * @return ApiMessage
	 * @throws ApiException
	 */
	public static Map<String, String> sign(double total_fee, String spbill_create_ip, String name,
			String trade_type, String openid) throws ApiException {
		SortedMap<String, Object> signMap = new TreeMap<String, Object>();
		// 微信开放平台审核通过的应用APPID
		signMap.put("appid", propertiesLoader.getProperty("wx_public_appid"));
		// 微信商户号
		signMap.put("mch_id", propertiesLoader.getProperty("wx_public_MchId"));
		// 随机字符串
		signMap.put("nonce_str", SeemseUtil.randoms().toUpperCase());
		// 商品描述
		signMap.put("body", name);
		// 用户唯一标识符
		signMap.put("openid", openid);
		// 商户订单号
		signMap.put("out_trade_no", SeemseUtil.getCurrentTime() + "W");
		// 总金额
		signMap.put("total_fee", (int) (total_fee * 100));
		signMap.put("sign_type", "MD5");
		// 用户端实际ip
		signMap.put("spbill_create_ip", spbill_create_ip);
		// 异步通知地址
		if (propertiesLoader.getProperty("sys.price.type").equals("0")) {
			//测试回调
			signMap.put("notify_url", propertiesLoader.getProperty("notify.url.wx.h5.test"));
		}else {
			//生产回调
			signMap.put("notify_url", propertiesLoader.getProperty("notify.url.wx.h5"));
		}
		// 交易类型
		signMap.put("trade_type", trade_type);
		String sign = WxPaySign.createSign(signMap, propertiesLoader.getProperty("wx_public_key"));
		signMap.put("sign", sign);
		return createPrePayOrder(signMap, trade_type, openid);

	}


}
